$(function () {
    // 检测登录
    $.ajax({
        url: "/employee/checkRootLogin",
        type: "GET",
        data: {},
        success: function (backdata) {
            console.log(backdata);
            if (backdata.success) {
                window.location.href = "index.html";
            }
        }
    })

    // 登录按钮
    // $("#dologin").click(function () {
    //     var username = $("#inputusername").val();
    //     var password = $("#inputPassword").val();
    //     $.ajax({
    //         url: "/employee/employeeLogin",
    //         type: "POST",
    //         data: {
    //             "username": username,
    //             "password": password
    //         },
    //         success: function (backdata) {

    //             // console.log(backdata.success);
    //             if (backdata.success) {
    //                 window.location.href = "index.html";
    //             }         
    //         }
    //     })
    // })

    var $form = $("form");
    $form.bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },

        fields: {
            //多个重复
            username: {
                validators: {
                    //1. 规则,验证是否输入了字符
                    notEmpty: {
                        message: "用户名不能为空"
                    },

                    //2. 规则2
                    stringLength: {
                        min: 4,
                        max: 20,
                        message: '用户名长度必须在4到20之间'
                    },

                    //3. 规则3
                    //正则校验
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '用户名由数字字母下划线和.组成'
                    },

                    callback: {
                        message: "用户名不存在"
                    }
                }
            },

            password: {
                validators: {
                    //1. 规则,验证是否输入了字符
                    notEmpty: {
                        message: "密码不能为空"
                    },

                    //2. 规则2
                    stringLength: {
                        min: 6,
                        max: 12,
                        message: '密码长度必须在6到12之间'
                    },

                    //3. 规则3
                    //正则校验
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '密码由数字字母下划线和.组成'
                    },

                    callback: {
                        message: "密码错误"
                    }
                }
            }
        }

    });

    $form.on("success.form.bv", function(target){
        target.preventDefault();

        $.ajax({
            url:"/employee/employeeLogin",
            type:"POST",
            data: $form.serialize(),
            success: function (data) {
                console.log(data);
                if (data.success) {
                    // window.location.href = window.sessionStorage.getItem("href");
                    window.location.href = "index.html";
                }
                if (data.error == 1000) {
                    var validator = $form.data("bootstrapValidator");
                    validator.updateStatus("username","INVALID","callback");
                }
                if (data.error == 1001) {
                    var validator = $form.data("bootstrapValidator");
                    validator.updateStatus("password","INVALID","callback");
                }
            }

        })

    })


    


})